import positionTpl from '../views/position.html'
import positionAddTpl from '../views/position_add.html'
import positionupdateTpl from '../views/position_update.html'
import handlerbar from 'handlebars';
class PositionController {
	constructor() {
		this.searchParams = {
			pageno: 1,
			pagesize: 5,
			keywords: ''
		}
		this._events();
		this._socket();
	}
	render(req, res, next) {
		positionController.res = res;
		if (req.query) {
			positionController.searchParams = {
				pageno: req.query.pageno,
				pagesize: req.query.pagesize,
				keywords: req.query.keywords
			}
		}
		positionController._renderlist(res);
	}
	addRender(req, res) {
		res.render(positionAddTpl);
	}
	updateRender(req, res) {
		console.log(req.query)
		positionController._updateView(req, res)
	}

	_events() {
		//添加职位按钮点击
		$("#content").on('click', 'button[name="position_add"]', () => {
			//重定向
			positionController.res.redirect('/position_add');
		})
		//点击返回按钮
		$("#content").on('click', 'button[name="position_back"]', () => {
			positionController.res.redirect('/position');
		})
		//点击提交按钮
		$("#content").on('click', 'button[name="position_submit"]', () => {
			this._addsubmit();
		})
		//点击编辑按钮
		$("#content").on('click', 'button[name="position_update"]', function() {
			positionController.res.redirect('/position_update?id=' + $(this).parent().attr("data-id"));
		})
		//从编辑界面返回
		$("#content").on('click', 'button[name="update_back"]', function() {
			positionController.res.redirect('/position');
		})
		//点击删除按钮
		$("#content").on('click', 'button[name="position_del"]', async function() {
			console.log($(this).parent().attr("data-id"))
			var rs = await positionController._delSubmit($(this).parent().attr("data-id"));
			if (rs.code) {
				alert(rs.msg);
				positionController._renderlist(positionController.res);
			}
		})
		//点击编辑界面的提交按钮
		$("#content").on('click', 'button[name="update_submit"]', function() {
			positionController.updateSubmit();
		})
		//点击搜索按钮
		$("#content").on('click', 'button[name="position_search"]', function() {
			positionController.searchParams.keywords = $(this).parent().prev().val();
			positionController._renderlist();
		})
		

	}
	//加载职位列表
	async _renderlist(res) {
		let rs = await positionController._load()
		if (!rs.code) {
			alert(rs.msg);
			res.redirect('/');
			return;
		}

		let num = () => {
			let result = [];
			for (let i = 1; i < Math.ceil(rs.total / 5) + 1; i++) {
				result.push(i);
			}
			return result;
		}

		let html = handlerbar.compile(positionTpl)({
			datalist: rs.datalist,
			pages: num(),
			pagesize: positionController.searchParams.pagesize,
			keywords: positionController.searchParams.keywords
		});
		this.res.render(html);
	}

	//ajaxSubmit方法提交表单
	_addsubmit() {
		$("#possave").ajaxSubmit({
			success: (result) => {
				if (result.code === 1) {
					alert(result.msg);
					positionController.res.redirect('/position');
				}

			}
		})
	}
	//获取position数据
	_getPosition() {
		let url = '/api/position/getPosition';
		$.ajax({
			type: 'get',
			url: url,
			success: (result) => {
				positionController.posData.positions = result.data;
			},
			error() {}
		})
	}

	//获取职位数据
	_load() {
		return new Promise((resolve, reject) => {
			let url = '/api/position/';
			$.ajax({
				type: 'get',
				url: url,
				data: positionController.searchParams,
				success: (result) => {
					resolve(result);
				},
				error() {

				}
			})
		})

	}

	//加载编辑界面
	_updateView(req, res) {
		let url = '/api/position/findOne?id=' + req.query.id;
		$.ajax({
			type: 'get',
			url: url,
			success: (result) => {
				let html = handlerbar.compile(positionupdateTpl)(result[0]);
				res.render(html);
			},
			error() {}
		})

	}

	//删除请求
	_delSubmit(id) {
		return new Promise((resolve, reject) => {
			let url = '/api/position/?id=' + id;
			$.ajax({
				type: 'delete',
				url: url,
				success: (result) => {
					resolve(result);
				},
				error() {

				}
			})
		});

	}
	
	//更新职位信息
	updateSubmit(){
		$("#posupdate").ajaxSubmit({
			success: (result) => {
				if(result.code == 1){
					positionController.res.redirect('/position')
				}
			}
		})
	}

	_socket() {
		var socket = io.connect('http://localhost:3000');
		socket.on('message', function(msg) {
			console.log(msg);
		})
	}

}

const positionController = new PositionController();

export default positionController;
